package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javabean.Permission;
import com.javabean.Role;
import com.javabean.RolePermission;
import com.service.PermissionService;
import com.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: 张波
 * @Time: 2025/8/28 14:35
 */
@Controller
@RequestMapping("/role")
public class RoleController {
	private final RoleService roleService;
	private  final PermissionService permissionService;

	public RoleController(RoleService roleService, PermissionService permissionService) {
		this.roleService = roleService;
		this.permissionService = permissionService;
	}
	@RequestMapping("/findAll")
	public String findAll(HttpServletRequest request, @RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "5")  int limit){
		PageHelper.startPage(page,limit);
		List<Role> all = roleService.findAll();
		PageInfo<Role> rolePageInfo = new PageInfo<>(all);
		request.setAttribute("pageInfo",rolePageInfo);
		return "/role/role-list";
	}
	@RequestMapping("/show")
	public String show(HttpServletRequest request,int id){
		Role byId = roleService.show(id);
		request.setAttribute("role",byId);
		return "role/role-show";
	}
	@RequestMapping(value = "/add",method = RequestMethod.GET)
	public String add(){
		return "/role/role-add";
	}
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public String add(Role role){
		roleService.addRole(role);
		return "redirect:findAll";
	}
	@RequestMapping(value = "/delete")
	public String delete(int id){
		roleService.deleteRole(id);
		return "redirect:findAll";
	}
	@RequestMapping(value = "/edit",method = RequestMethod.GET)
	public String updateById(HttpServletRequest request,int id){
		Role role = roleService.show(id);
		request.setAttribute("role",role);
		return "/role/role-edit";
	}
	@RequestMapping(value = "/edit",method = RequestMethod.POST)
	public String update(Role role){
		roleService.updateRole(role);
		return "redirect:findAll";
	}
	@RequestMapping(value = "/permissionAdd",method = RequestMethod.GET)
	public String permissionAdd(HttpServletRequest request,int id){
		List<Permission> all = permissionService.findAll();
		List<Permission> byRid = permissionService.findByRid(id);
		request.setAttribute("roleId", id);
		request.setAttribute("permissionList", all);
		request.setAttribute("permissionIdList", byRid);
		return "/role/role-edit-permission";
	}
	@RequestMapping(value = "/permissionAdd",method = RequestMethod.POST)
	public void permissionAdd(@RequestBody RolePermission rolePermission){
		roleService.permissionAdd(rolePermission);
	}
}
